var game = function(){
    var html = '<div style="margin: 10px">\n' +
        '<div style="margin-top: 10px;margin-right: 20px">\n' +
        // '    <form class="layui-form" action="javascript:;" id="selectStockForm">\n' +
        // '        <div class="layui-form-item">\n' +
        // '            <div class="layui-inline">\n' +
        // '                <label class="layui-form-label">游戏名</label>\n' +
        // '                <div class="layui-input-inline">\n' +
        // '                    <input type="text" name="keyword" id="keyword" autocomplete="off" placeholder="请输入游戏名"\n' +
        // '                           class="layui-input">\n' +
        // '                </div>\n' +
        // '            </div>\n' +
        // '        </div>\n' +
        // '            <div class="layui-form-item">\n' +
        // '                <div class="layui-inline">\n' +
        // '                    <label class="layui-form-label">起始时间</label>\n' +
        // '                    <div class="layui-input-inline">\n' +
        // '                        <input type="text" name="beginDate" id="date1" lay-verify="datetime" placeholder="起始时间"\n' +
        // '                               autocomplete="off" class="layui-input">\n' +
        // '                    </div>\n' +
        // '                </div>\n' +
        // '                <div class="layui-inline">\n' +
        // '                    <label class="layui-form-label">截至时间</label>\n' +
        // '                    <div class="layui-input-inline">\n' +
        // '                        <input type="text" name="endDate" id="date2" lay-verify="datetime" placeholder="结束时间"\n' +
        // '                               autocomplete="off" class="layui-input">\n' +
        // '                    </div>\n' +
        // '                </div>\n' +
        // '                <div class="layui-inline">\n' +
        // '                    <div class="layui-input-block">\n' +
        // '                        <button class="layui-btn" lay-submit="" lay-filter="demo2">立即搜索</button>\n' +
        // '                    </div>\n' +
        // '                </div>\n' +
        // '            </div>\n' +
        // '    </form>\n' +
        // '</div>\n' +
        '    <hr style="margin-bottom: 20px">\n' +
        '    <div style="margin-left: 10px;margin-right: 10px">\n' +
        '        <table class="layui-hide" id="demo" lay-filter="test"></table>\n' +
        '    </div>\n' +
        '    <input id="addProductHidden" value="" hidden="hidden">\n' +
        '</div>';
    layui.use(['table', 'form', 'jquery', 'element','layer', 'laydate'], function () {
        var table = layui.table
            ,layer = layui.layer
            , form = layui.form
            , $ = layui.jquery
            , laydate = layui.laydate
            , element = layui.element;
        $("#mainbody").html(html);
        //日期
        laydate.render({
            elem: '#date1'
            , type: 'datetime'
        });
        laydate.render({
            elem: '#date2'
            , type: 'datetime'
        });

        var tableRender = function () {
            //执行一个 table 实例

            table.render({
                elem: '#demo'
                // , height: 420
                , id:"gameList"
                , url: '/game/getList' //数据接口
                , title: '游戏表'
                , page: true //开启分页
                , toolbar: 'default'
                , limit: 10
                , limits: [10, 20, 30]
                , cols: [[ //表头
                    {type: 'checkbox'}
                    ,{type: 'numbers',title: '序号',width: 80}
                    , {field: 'gameKindName', align: 'center', title: '分类', width: 130}
                    , {field: 'gamePlatformName', align: 'center', title: '平台', width: 130}
                    , {field: 'gameName', align: 'center', title: '游戏名称', width: 130}
                    , {field: 'gameXima', align: 'center', title: '洗码比例', width: 130}
                    , {field: 'gameCreateTime', align: 'center', title: '创建时间', width: 180}
                    , {
                        field: 'switch', title: '启用/禁用', align: 'center', width: 90, templet: function (res) {
                            if (res.gameStatus == 0) {
                                return '<input value="'+res.gameId+'" hidden="hidden"><input type="checkbox"  lay-skin="switch" lay-filter=test1 lay-text="上架|下架"" checked>';
                            } else {
                                return '<input value="'+res.gameId+'" hidden="hidden"><input type="checkbox"  lay-skin="switch" lay-filter=test1 lay-text="上架|下架"">';
                            }
                        }
                    }

                ]]
            });
        }

        //初始化表格
        tableRender();

        var openAddGame = function () {
            layer.open({
                title: ['添加游戏', 'font-size:18px;']
                , shade: 0.6 //遮罩透明度
                , area: '400px'//宽度
                , type: 2
                , anim: 1 //0-6的动画形式，-1不开启
                , content: 'jump?url=game/addGame'
                , success: function (layero, index) {
                    //layer高度自适应
                    layer.iframeAuto(index);
                }
                ,end: function (data) {
                    table.reload('gameList');
                }
            })
        }

        var openDeleteGame = function(json){
            layer.confirm('确定删除勾选数据吗？', {icon: 3, title:'提示'}, function(index){
                var arr = new Array();
                for(var i =0 ;i < json.length;i++){
                    arr.push(json[i].gameId);
                }
                $.ajax({
                    type: "POST",
                    url: "/game/delete",
                    traditional: true,
                    data: JSON.stringify(arr),
                    // dataType: "json",
                    contentType: "application/json",
                    success: function (data) {
                        layer.msg('删除成功！', {
                            icon: 1,
                            time: 1000 //2秒关闭（如果不配置，默认是3秒）
                        });
                        table.reload("gameList");
                    },
                    error: function () {
                        console.log("调用删除方法失败！");
                    }
                });
            });
        }
        //监听头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id)
                ,data = checkStatus.data; //获取选中的数据
            switch(obj.event){
                case 'add':
                    openAddGame();
                    break;
                case 'update':
                    var data = checkStatus.data;
                    if(data.length<1){
                        layer.msg('未选择数据！',
                            {icon: 2, time: 500}
                        )
                    }else{
                        layer.open({
                            title: ['添加游戏', 'font-size:18px;']
                            , shade: 0.6 //遮罩透明度
                            , area: '400px'//宽度
                            , type: 2
                            , anim: 1 //0-6的动画形式，-1不开启
                            , content: 'game/editButton/'+data[0].gameId
                            , success: function (layero, index) {
                                //layer高度自适应
                                layer.iframeAuto(index);
                            }
                            , end: function (data) {
                                table.reload('gameList');
                            }
                        })
                    }
                    break;
                case 'delete':
                    var data = checkStatus.data;
                    openDeleteGame(data);
                    break;
            };
        });

        form.on('switch(test1)', function (data) {
            var x = data.elem.checked;
            if (x) {
                handleSwitch("确定启用吗？", data, x);
            } else {
                handleSwitch("确定禁用吗？", data, x);
            }


        });

        var handleSwitch = function (content, data, isChecked) {
            var tableDiv = $(data.othis);
            var tableId = tableDiv.parent().find("input").eq(0).val();
            var tableBut = tableDiv.find("em").html();
            var tableButId;
            if (tableBut=="上架"){
                tableButId = 0;
            }
            if (tableBut=="下架") {
                tableButId = 1;
            }
            var mydata = {
                gameId: tableId,
                gameStatus: tableButId
            };
            var test = JSON.stringify(mydata)
            layer.open({
                content: content
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    $.ajax({
                        url:'/game/add',
                        data:test,
                        // dataType:'text',
                        type:'post',
                        contentType: "application/json",
                        success:function (data) {
                            console.log(data);
                            if (data == 'success'){
                                table.reload('gameList');
                            }else{
                                layer.msg('错误');
                            }
                        },
                        error: function (data) {

                            layer.msg(JSON.parse(data.responseText).message,{ icon: 1, time: 1000 });
                        }
                    });
                    data.elem.checked = isChecked;
                    form.render();
                    layer.close(layer.index);
                    //按钮【按钮一】的回调
                }
                , btn2: function (index, layero) {
                    //按钮【按钮二】的回调
                    data.elem.checked = !isChecked;
                    form.render();
                    layer.close(layer.index);
                    //return false 开启该代码可禁止点击该按钮关闭
                }
                , cancel: function () {
                    //右上角关闭回调
                    data.elem.checked = !isChecked;
                    form.render();
                    //return false 开启该代码可禁止点击该按钮关闭
                }
            });
            return false;
        }




/*


        //监听指定开关
        form.on('switch(test1)', function (obj) {
            //开关颜色变红
            $(obj.othis).css("border-color","red");
            $(obj.othis).css("background-color","red");
        });


        //监听搜索按钮
        form.on('submit(demo2)', function (data) {
            var status = data.field.productStatus;
            //layer.msg(data);
            var keyword = $("#keyword").val();
            var productNumber = $("#productNumber").val();
            var begin = $("#date1").val();
            var end = $("#date2").val();
            table.reload('productList', {
                where: {"keyword": keyword, "beginStr": begin, "endStr": end, "productStatus": status, "productNumber": productNumber}
            });
        });

    })*/
    })
}